//package com.yuxi.distributelock;
//
//import net.rubyeye.xmemcached.GetsResponse;
//import net.rubyeye.xmemcached.MemcachedClient;
//import net.rubyeye.xmemcached.XMemcachedClient;
//import net.rubyeye.xmemcached.exception.MemcachedException;
//
//import java.net.InetSocketAddress;
//import java.util.concurrent.CountDownLatch;
//import java.util.concurrent.TimeoutException;
//
///**
// * Created by yuxi on 16/12/26.
// * cas  gets
// */
//public class MemcacheGetsCas {
//    static CountDownLatch countDownLatch = new CountDownLatch(20);
//
//    public static void main(String[] args) throws Exception {
//        MemcachedClient cache = new XMemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
//        cache.delete("key");
//        for (int i = 0; i < 10; i++) {
//            new MThread("A", cache, countDownLatch).start();
//            Thread.sleep(50);
//            new MThread("B", cache, countDownLatch).start();
//        }
//        countDownLatch.await();
//        Thread.sleep(50);
//        System.out.println("=========" + cache.gets("key"));
//    }
//
//
//    static class MThread extends Thread {
//        private MemcachedClient cache;
//        private CountDownLatch countDownLatch;
//
//        public MThread(String name, MemcachedClient cache, CountDownLatch countDownLatch) throws Exception {
//            super(name);
//            this.cache = cache;
//            this.countDownLatch = countDownLatch;
//        }
//
//        @Override
//        public void run() {
//            try {
//                GetsResponse obj = cache.gets("key");
//                if (obj == null) {
//                    cache.set("key", 0, 1);
//                } else {
//                    int time = 0;
//                    while (time < 5) {
//                        time++;
//                        long l = obj.getCas();
//                        boolean success = cache.cas("key", 0, "3", l);
//                        if (success) {
//                            System.out.println("gets success====i===l=====" + time + "===" + success+"======"+l);
//                            break;
//                        }
//                    }
//                }
//            } catch (TimeoutException e) {
//                e.printStackTrace();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (MemcachedException e) {
//                e.printStackTrace();
//            } finally {
//                countDownLatch.countDown();
//                System.out.println("=======coundown=======");
//            }
//        }
//    }
//}
//
//
